<?php
/**
 * Create By: PhpStorm
 * User: yxc
 * File: UserAccount .php
 * Date: 2017/1/6
 * Time: 0:00
 */
namespace app\api\model\page\user;

use app\api\model\dao\Ad;
use app\api\model\dao\Banner;
use app\api\model\dao\UserAccount;
use app\api\model\dao\UserCoupon;
use app\api\model\dao\UserCurrency;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Validate;

class GetUserInfo extends PageBase
{

    private $objUserAccountModel;
    private $objUserCurrency;
    private $objUserCoupon;
    private $objAd;

    public function __construct()
    {
        parent::__construct();
        $this->objUserAccountModel = new UserAccount();
        $this->objUserCurrency = new UserCurrency();
        $this->objUserCoupon = new UserCoupon();
        $this->objAd = new Ad();
    }

    public function excute($param)
    {
        $data = [
            'userid' => '0',
            'nickname' => '',
            'user_bean' => '0',
            'user_coin' => '0',
            'user_coupon' => '0',
            'banner' => []
        ];

        $ad = $this->objAd->getAdByPid(16);
        if (!empty($ad)) {
            foreach ($ad as $val) {
                if (0 != $val['status']) {
                    continue;
                }
                $data['banner'][] = [
                    'pic' => $val['ad_code'],
                    'type' => strval($val['media_type']),
                    'content' => $val['ad_link'],
                ];
            }
        }

        $ret = $this->checkToken($param);
        if (false === $ret) {
            $this->data = $data;
            return $this->getRes();
        }

        $user_coin = $this->objUserCurrency->getCurrencyByUserid($this->user_id, Flag::CURRENCY_TYPE_COIN);
        if (!empty($user_coin)) {
            $user_coin = $this->modelToArray($user_coin);
            $data['user_coin'] = $user_coin['amount'];
        }

        $user_bean = $this->objUserCurrency->getCurrencyByUserid($this->user_id, Flag::CURENCY_TYPE_INTEGRAL);
        if (!empty($user_bean)) {
            $user_bean = $this->modelToArray($user_bean);
            $data['user_bean'] = $user_bean['amount'];
        }

        $user_coupon = $this->objUserCoupon->getCouponByUserid($this->user_id);
        if (!empty($user_coupon)) {
            $data['user_coupon'] = count($user_coupon);
        }

        $this->data = $data;

        return $this->getRes();
    }
}


